/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mailctrl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author flintx
 */
public class MailLocalConf {

    Element root;
    String configFile = "";
    String mailFile = "";
    Document document = null;


    public void setFileConf(String file)
    {
        this.configFile = file;
    }

    public void setFileMail(String file)
    {
        this.mailFile = file;
    }
    public Element packageMailInfo(Date date, String content)
    {
        Element appInfo = new Element("mail");
        appInfo.addContent(new Element("Date").setText(date.toString()));
        appInfo.addContent(new Element("content").setText(content));
        return appInfo;
    }
    public Element packageAppInfo(String appName, String appPath, List<String> attribute)
    {
        Element appInfo = new Element("application");
        appInfo.addContent(new Element("Name").setText(appName));
        appInfo.addContent(new Element("Path").setText(appPath));
        Element attr = new Element("Parameters");
        attr.setAttribute("Total", ""+attribute.size());
        for(int i=0; i< attribute.size(); i++)
        {
            attr.addContent(new Element("parameter").setText(attribute.get(i)));
        }
        appInfo.addContent(attr);
        return appInfo;
    }

    public void loadMailPathFile()
    {
        SAXBuilder builder = new SAXBuilder(false);
        try{
            File file = new File(mailFile);
            if(!file.exists())
            {
                file.createNewFile();
                root = new Element("root");
                document = new Document(root);
                Element apps = new Element("mails");
                root.addContent(apps);
            }
            else
            {
                document = (Document)builder.build(file);
                root = document.getRootElement();
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
        catch (JDOMException ex)
        {
            ex.printStackTrace();
        }
    }
    
    public void loadAppPathFile() {
        SAXBuilder builder = new SAXBuilder(false);
        try {
            File file = new File(configFile);
            if(!file.exists())
            {
                file.createNewFile();
                root = new Element("root");
                document = new Document(root);
                Element apps = new Element("applications");
                root.addContent(apps);

            }
            else
            {
                document = (Document)builder.build(file);
                root = document.getRootElement();
            }
        } catch (JDOMException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void savePathFile(String localfile) {
        try {
            Format format = Format.getCompactFormat();
            format.setEncoding("gb2312");           //设置xml文件的字符为gb2312
            format.setIndent("    ");               //设置xml文件的缩进为4个空格
            XMLOutputter XMLOut = new XMLOutputter(format);
            XMLOut.output(document, new FileOutputStream(localfile));
        } catch (IOException ex) {
            ex.printStackTrace();
        } 
    }

    public void addMailInfo(Element newMail)
    {
        Element mails = root.getChild("mails");
        List mailsList = mails.getChildren("mail");
        String mailTime = newMail.getChildText("Date");
        for(int i=0; i<mailsList.size(); i++)
        {
            Element mail = (Element)mailsList.get(i);
            if((mail.getChildText("Date")).equals(mailTime))
            {
                return;
            }
        }
        mails.addContent(newMail);
    }
    
    public void addAppInfo(Element newApp)
    {
        Element apps = root.getChild("applications");
        List appList = apps.getChildren("application");
        String appName = newApp.getChild("Name").getValue();
        for(int i=0; i<appList.size(); i++)
        {
            Element app = (Element)appList.get(i);
            if(((app.getChild("Name")).getValue()).equals(appName))
            {
                return;
            }
        }
        apps.addContent(newApp);
        
    }

    public void modifyAppInfo(String appName, Element newConf)
    {
        Element apps = root.getChild("applications");
        List appList = apps.getChildren("application");
        for(int i=0; i<appList.size(); i++)
        {
            Element app = (Element)appList.get(i);
            if(((app.getChild("Name")).getValue()).equals(appName))
            {
                apps.removeContent(app);
                apps.addContent(newConf);
                break;
            }
        }
    }

    public void delAppInfo(String appName)
    {
        Element apps = root.getChild("applications");
        List appList = apps.getChildren("application");

        for(int i=0; i<appList.size(); i++)
        {
            Element app = (Element)appList.get(i);
            if(((app.getChild("Name")).getValue()).equals(appName))
            {
                apps.removeContent(app);
                break;
            }
        }
    }
}
